package trb.complexshield.bool;


import no.hrp.hvrc.rad.calc.intersect.Segment;
import no.hrp.hvrc.rad.dosim.bvh.BottumUpBVH;
import trb.complexshield.ComplexShield;

public class ComplexShieldSegmentsProducer implements ComplexShield {

    private SegmentsProducer segmentsProducer;
    private final BottumUpBVH.AABB aabb;
    private final SegmentsMemory memory = new SegmentsMemory();

    public ComplexShieldSegmentsProducer(SegmentsProducer segmentsProducer, BottumUpBVH.AABB aabb) {
        this.segmentsProducer = segmentsProducer;
        this.aabb = aabb;
    }

    @Override
    public void intersect(Segment segment, Output output) {
        if (!aabb.intersects(segment)) {
            return;
        }

        memory.clear();
        Segments segmentsOut = new Segments();
        segmentsProducer.getSegments(segment, segmentsOut, memory);
        for (int i=0; i<segmentsOut.segmentCount; i++) {
            output.addIntersection(segmentsOut.data[i*2], segmentsOut.data[i*2+1]);
        }
    }
}
